package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.cqut.service.IRoleService;
import com.cqut.service.IRoleRigthService;
import com.cqut.dto.LimitShowDTO;
import com.cqut.dto.RoleDTO;
import com.cqut.dto.RoleRigthDTO;
import com.cqut.util.ResponseUtil;


@RestController
@RequestMapping("/api/role")
public class RoleController {
	
    @Resource(name="roleService")
    private IRoleService roleService;

    @Resource(name="roleRigthService")
    private IRoleRigthService roleRigthService;
    
    /**
     * 初始化列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
    	String roleName,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> roleList = roleService.findRolesWithLimit(roleName,limitShowDTO);
        long count = roleService.findCountOfRoles(roleName);

        result.put("rows", roleList);
        result.put("total", count);
        System.out.println(roleList.toString());

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    /**
     * 新增一个角色
     * @param roleDTO
     * @param roleRigthDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    public ResponseEntity<String> create(
    	@Validated
        RoleDTO roleDTO,
        @Validated
        RoleRigthDTO roleRigthDTO,BindingResult errors) {
    	
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
            
		if(roleService.addRoleAndRolePermission(roleDTO,roleRigthDTO,roleRigthService.getMaxRoleRightId()))
			return ResponseUtil.getResEntityForPPPWhenSuccess();
		
        return ResponseUtil.getResEntityForPPPWhenFail();
    }
    
    /**
     * 修改一个角色
     * @param roleDTO
     * @param roleRigthDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/updateRole", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
    	@Validated
        RoleDTO roleDTO,
        @Validated
        RoleRigthDTO roleRigthDTO,BindingResult errors) {
    	
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        
       /* if(roleRigthService.delete(roleDTO.getRoleId())){
        	if(roleService.upadateRoleAndRolePermission(roleDTO,roleRigthDTO,roleRigthService.getMaxRoleRightId()))
    			return ResponseUtil.getResEntityForPPPWhenSuccess();
        }*/
        roleRigthService.delete(roleDTO.getRoleId());
        if(roleService.upadateRoleAndRolePermission(roleDTO,roleRigthDTO,roleRigthService.getMaxRoleRightId()))
			return ResponseUtil.getResEntityForPPPWhenSuccess();
        	
		return ResponseUtil.getResEntityForPPPWhenFail();
    }
    

	/**
	 * 获取角色总数
	 * @param roleName
	 * */
	@RequestMapping(value="/count", method=RequestMethod.GET)
	public ResponseEntity<String> findCountOfRolesWithLimit(String roleName) {
		JSONObject json = new JSONObject();
		json.put("total", roleService.findCountOfRoles(roleName));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
	
	/**
	 * 操作员新增(操作员未写DTO)
	 */
	@RequestMapping(value = "addOperator", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        String name,String password,String sex,String state,String type,String phone,String email,String college) {

        return ResponseUtil.getResEntityForPPP(roleService.addOperator(name,password,sex,state,type,phone,email,college));
    }
	
	/**
	 * 通过id数组删除角色
	 * @param roleIds
	 * */
	@RequestMapping(value="/rolesDel", method=RequestMethod.POST)
	public ResponseEntity<String> deleteRoleByIds(
			@RequestParam("roleIds")
			String[] roleIds) {
		if(roleIds == null || 0 == roleIds.length)
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		if(roleService.deleteRoleByIds(roleIds)&&roleRigthService.batchDelete(roleIds))
			return ResponseUtil.getResEntityForDelWhenSuccess();
		else return ResponseUtil.getResEntityForDelWhenFail();
	}
	
	/**
	 * 通过id查询角色权限
	 * @param roleId
	 * */
	@RequestMapping(value="/roleDetail", method=RequestMethod.POST)
	public ResponseEntity<String> findRolePermById(
			@RequestParam("roleId") String roleId) {
		if(roleId == null || 0 == roleId.length())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		List<Map<String, Object>> rs = roleRigthService.findPermById(roleId);
		
		return ResponseUtil.getResEntityForGetAndJsonArray(rs);
	}
	
	/**
	 * 通过id查询角色信息
	 * @param roleId
	 * */
	@RequestMapping(value="/idDetail", method=RequestMethod.POST)
	public ResponseEntity<String> findRoleById(
			@RequestParam("roleId") String roleId) {
		if(roleId == null || 0 == roleId.length())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		Map<String, Object> role = roleService.findMap(roleId);
		return ResponseUtil.getResEntityForGetAndJson(role);
	}
	
	/**
	 * 通过roleId获得相应权限id
	 * @param roleId
	 * **/
	@RequestMapping(value="/permissionsIds", method=RequestMethod.POST)
	public ResponseEntity<String> findPermissionsIdsByroleId(
			@RequestParam("roleId") String roleId) {
		if(roleId == null || 0 == roleId.length())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		List<Map<String, Object>> ids = roleRigthService.findPermissionIds(roleId);
		return ResponseUtil.getResEntityForGetAndJsonArray(ids);
	}
	
}
